<template>
    <div>
         <!-- 面包屑导航区 -->
        <el-breadcrumb separator-stu_grants="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>学生管理</el-breadcrumb-item>
            <el-breadcrumb-item>助学金列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="学生助学金" name="first">
                    <!-- 搜索 添加 -->
                    <el-row :gutter="20">
                        <el-col :span="10">
                            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getStuGrantsList">
                                <el-button type="success" slot="append" icon="el-icon-search" @click="getStuGrantsList"></el-button>
                            </el-input>
                        </el-col>
                        <el-col :span="13">
                            <el-button type="primary" @click="showAddStuGrantDialog" style="float:right">添加学生助学金</el-button>
                        </el-col>
                    </el-row>
                    <el-table :data="stu_grants_list" border stripe :header-cell-style="table_header" empty-text="暂无数据">
                        <!-- 索引列 -->
                        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                        <el-table-column label="学号" prop="stu_id" align="center" width="120px"></el-table-column>
                        <el-table-column label="姓名" prop="student.name" align="center" min-width="100px"></el-table-column>
                        <el-table-column label="助学金名称" prop="grant.name" align="center" width="150px"></el-table-column>
                        <el-table-column label="等级" prop="grant_level" align="center"></el-table-column>
                        <el-table-column label="金额" prop="grant_amount" align="center"></el-table-column>
                        <el-table-column label="获得时间" prop="grant_time" align="center">
                            <template slot-scope="scope">{{scope.row.grant_time | dateFormat}}</template>
                        </el-table-column>
                        <el-table-column label="是否贷款" prop="is_loan" align="center">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.is_loan === 1">是</el-tag>
                                <el-tag v-else class="danger">否</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="200px" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditStuGrantDialog(scope.row)">编辑</el-button>
                                <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeStuGrantById(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!-- 分页区域 -->
                    <div stu_grants="pager-box">
                        <el-pagination
                                @size-change="handleSizeChange"
                                @current-change="handleCurrentChange"
                                :current-page="queryInfo.pageNum"
                                :page-sizes="[2, 5, 10, 15]"
                                :page-size="queryInfo.pageSize"
                                layout="total, sizes, prev, pager, next, jumper"
                                prev-text="上一页"
                                next-text="下一页"
                                :total="total"
                                background
                                hide-on-single-page
                        ></el-pagination>
                </div>
                    <!-- 编辑学生助学金对话框 -->
                    <el-dialog :close-on-click-modal="false" :title="Dialog_title" :visible.sync="StuGrantDialogVisible" width="40%" @close="resetForm('StuGrantFormRef')">
                        <el-form
                                :model="StuGrantForm"
                                ref="StuGrantFormRef"
                                :rules="StuGrantFormRules"
                                label-width="100px"
                        >
                            <el-form-item label="学号" prop="StuGrant_name">
                                <el-select v-model.number="StuGrantForm.stu_id" filterable placeholder="请选择">
                                    <el-option
                                            v-for="item in stu_id_list"
                                            :key="item"
                                            :label="item"
                                            :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="助学金" prop="grant_id">
                                <el-select v-model.number="StuGrantForm.grant_id" filterable placeholder="请选择">
                                    <el-option
                                            v-for="item in grantList"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="助学金等级" prop="grant_level">
                                <el-input v-model="StuGrantForm.grant_level"></el-input>
                            </el-form-item>
                            <el-form-item label="助学金金额" prop="grant_amount">
                                <el-input v-model.number="StuGrantForm.grant_amount"></el-input>
                            </el-form-item>
                            <el-form-item label="获得时间" prop="grant_amount">
                                <el-date-picker v-model="StuGrantForm.grant_time" type="date" placeholder="选择日期"></el-date-picker>
                            </el-form-item>
                            <el-form-item label="是否贷款" prop="is_loan">
                                <el-select v-model.number="StuGrantForm.grant_id" filterable placeholder="请选择">
                                    <el-option label="是" :value="1"></el-option>
                                    <el-option label="否" :value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                        <span slot="footer" class="dialog-footer">
                            <el-button @click="StuGrantDialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="AddOrUpdateStuGrant">确 定</el-button>
                        </span>
                    </el-dialog>
                </el-tab-pane>
                <el-tab-pane label="助学金管理" name="second">
                    <!-- 搜索 添加 -->
                    <el-row :gutter="20">
                        <el-col :span="10">
                            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getStuGrantsList">
                                <el-button type="success" slot="append" icon="el-icon-search" @click="getStuGrantsList"></el-button>
                            </el-input>
                        </el-col>
                        <el-col :span="13">
                            <el-button type="primary" @click="showAddGrantDialog" style="float:right">添加助学金</el-button>
                        </el-col>
                    </el-row>
                    <el-table :data="grantList" border stripe :header-cell-style="table_header" empty-text="暂无数据">
                        <!-- 索引列 -->
                        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                        <el-table-column label="助学金名称" prop="name" align="center"></el-table-column>
                        <el-table-column label="操作" width="300px" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditGrantDialog(scope.row)">编辑</el-button>
                                <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGrantById(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-dialog
                            :close-on-click-modal="false"
                            :title="Dialog_title"
                            :visible.sync="GrantDialogVisible"
                            width="35%"
                            @close="resetForm('GrantFormRef')"
                    >
                        <el-form
                                :model="GrantForm"
                                ref="GrantFormRef"
                                :rules="GrantFormRules"
                                label-width="80px"
                        >
                            <el-form-item label="助学金称" prop="name">
                                <el-input v-model="GrantForm.name"></el-input>
                            </el-form-item>
                        </el-form>
                        <span slot="footer" class="dialog-footer">
                            <el-button @click="GrantDialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="AddOrUpdateGrant">确 定</el-button>
                        </span>
                    </el-dialog>
                </el-tab-pane>
            </el-tabs>
        </el-card>
    </div>
</template>

<script>
export default {
    data () {
        return {
            queryInfo: {
                query: '',
                // 当前页数
                pageNum: 1,
                // 每页显示多少数据
                pageSize: 10,
            },
            total: 0,
            // 所有学生助学金列表
            activeName: 'first',
            stu_grants_list: [],
            grantList: [],
            stu_id_list: [],

            Dialog_state: "",
            Dialog_title: "",

            StuGrantDialogVisible: false,
            StuGrantForm: {},
            StuGrantFormRules: {
                stu_id: [
                    { required: true, message: '请输入学号', trigger: 'blur' }
                ],
            },

            GrantDialogVisible: false,
            GrantForm: {},
            GrantFormRules: {
                name: [
                    { required: true, message: '请输入助学金名称', trigger: 'blur' }
                ],
            },
        }
    },
    created () {
        this.getData();
    },
    methods: {
        table_header(row, column, rowIndex, columnIndex){
            return 'background-color: rgb(102,177,255);color: white;font-size:14px;'
        },
        handleSizeChange (newSize) {
            this.queryInfo.pageSize = newSize;
            this.getStuGrantsList()
        },
        handleCurrentChange (newSize) {
            this.queryInfo.pageNum = newSize;
            this.getStuGrantsList()
        },
        handleClick(tab, event) {
            if (tab.paneName === 'first'){
                this.getStuGrantsList();
            }else if (tab.paneName === "second"){
                this.getGrantsList();
            }
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.StuGrantForm = {};
            this.GrantForm = {};
        },
        async getData(){
            await this.getGrantsList();
            this.getStuGrantsList();
            this.getStuIDList();
        },
        async getStuIDList() {
            const {data: res, status: status} = await this.axios.get('student/getStuIDList');
            if (status !== 200) {
                return this.$message.error('获取学生列表失败！')
            }
            this.stu_id_list = res.data;
        },
        async getStuGrantsList() {
            const { data: res, status: status } = await this.axios.get('stu_grants/getStuGrantList', {params: this.queryInfo})
            if (status !== 200) {
                return this.$message.error('获取学生助学金列表失败！')
            }
            this.stu_grants_list = res.data.stu_grants;
            this.total = res.data.total;
        },
        async getGrantsList() {
            const { data: res, status: status } = await this.axios.get('grants/getGrantList', {params: this.queryInfo})
            if (status !== 200) {
                return this.$message.error('获取助学金列表失败！')
            }
            this.grantList = res.data.grants;
            this.total = res.total;
        },

        showAddStuGrantDialog(){
            this.Dialog_state = "add";
            this.Dialog_title = "添加学生助学金";
            this.StuGrantDialogVisible = true
        },
        showEditStuGrantDialog (StuGrant) {
            this.Dialog_state = 'edit';
            this.Dialog_title = "编辑学生助学金";
            this.StuGrantForm = StuGrant;
            this.StuGrantDialogVisible = true
        },
        showAddGrantDialog(){
            this.Dialog_state = "add";
            this.Dialog_title = "添加助学金";
            this.GrantDialogVisible = true
        },
        showEditGrantDialog (Grant) {
            this.Dialog_state = 'edit';
            this.Dialog_title = "编辑助学金";
            this.GrantForm = Grant;
            this.GrantDialogVisible = true
        },
        
        // 添加学生助学金A
        AddOrUpdateStuGrant () {
            var stu_grants_form = JSON.parse(JSON.stringify(this.StuGrantForm));
            stu_grants_form.student = {};
            stu_grants_form.grant = {};
            if (this.Dialog_state === "add") {
                this.$refs.StuGrantFormRef.validate(async valid => {
                    if (!valid) return
                    const {data: res, status: status} = await this.axios.post('stu_grants/addStuGrant', stu_grants_form);
                    if (status !== 201) {
                        this.$message.error('添加学生助学金失败！')
                    }
                    this.$message.success('添加学生助学金成功！');
                    this.getStuGrantsList()
                })
            }else {
                this.$refs.StuGrantFormRef.validate(async valid => {
                    if (!valid) return;
                    const { data: res, status: status } = await this.axios.put('stu_grants/updateStuGrant', stu_grants_form);
                    if (status !== 200) {
                        this.$message.error('更新学生助学金信息失败！')
                    }
                    this.$message.success('更新学生助学金信息成功！');
                    this.getStuGrantsList()
                })
            }
            this.StuGrantDialogVisible = false
        },
        AddOrUpdateGrant () {
            var grants_form = JSON.parse(JSON.stringify(this.GrantForm));
            if (this.Dialog_state === "add") {
                this.$refs.GrantFormRef.validate(async valid => {
                    if (!valid) return;
                    const {data: res, status: status} = await this.axios.post('grants/addGrant', grants_form);
                    if (status !== 201) {
                        this.$message.error('添加助学金失败！')
                    }
                    this.$message.success('添加助学金成功！');
                    this.getGrantsList()
                })
            }else {
                this.$refs.GrantFormRef.validate(async valid => {
                    if (!valid) return
                    const { data: res, status: status } = await this.axios.put('grants/updateGrant', grants_form);
                    if (status !== 200) {
                        this.$message.error('更新助学金信息失败！')
                    }
                    this.$message.success('更新助学金信息成功！');
                    this.getGrantsList()
                })
            }
            this.GrantDialogVisible = false
        },
        // 删除学生助学金
        async removeStuGrantById (StuGrant) {
            const confirmResult = await this.$confirm(
                '此操作将永久删除学生助学金 ' + StuGrant.name +  ',  是否继续?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).catch(err => err);
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }
            const { data: res, status: status } = await this.axios.delete('stu_grants/deleteStuGrant', {params: {"id": StuGrant.id}});
            if (status !== 200) return this.$message.error('删除学生助学金失败！')
            this.$message.success('删除学生助学金成功！')
            this.getStuGrantsList()
        },
        async removeGrantById (Grant) {
            const confirmResult = await this.$confirm(
                '此操作将永久删除助学金 ' + Grant.name +  ',  是否继续?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).catch(err => err);
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }
            const { data: res, status: status } = await this.axios.delete('grants/deleteGrant', {params: {"id": Grant.id}});
            if (status !== 200) return this.$message.error('删除学生助学金失败！')
            this.$message.success('删除助学金成功！')
            this.getGrantsList()
        },
    } 
}
</script>
<style lang="less" scoped>

</style>